home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Demos / Donuts3D / gamemenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  4.1 KB  |  142 lines

  1. //-----------------------------------------------------------------------------
  2. // File: GameMenu.cpp
  3. //
  4. // Desc: Code for in-game menus
  5. //
  6. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #define D3D_OVERLOADS
  10. #include <D3D8.h>
  11. #include <D3DX8Math.h>
  12. #include <mmsystem.h>
  13. #include "D3DFont.h"
  14. #include "D3DUtil.h"
  15. #include "GameMenu.h"
  16.  
  17.  
  18.  
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Name: 
  22. // Desc: 
  23. //-----------------------------------------------------------------------------
  24. CMenuItem::CMenuItem( TCHAR* strNewLabel, DWORD dwNewID )
  25. {
  26.     _tcscpy( strLabel, strNewLabel );
  27.     dwID           = dwNewID;
  28.     pParent        = NULL;
  29.     dwNumChildren  = 0L;
  30.     dwSelectedMenu = 0L;
  31. }
  32.  
  33.  
  34.  
  35.  
  36. //-----------------------------------------------------------------------------
  37. // Name: 
  38. // Desc: 
  39. //-----------------------------------------------------------------------------
  40. CMenuItem::~CMenuItem()
  41. {
  42.     while( dwNumChildren )
  43.         delete pChild[--dwNumChildren];
  44. }
  45.  
  46.  
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Name: 
  51. // Desc: 
  52. //-----------------------------------------------------------------------------
  53. CMenuItem* CMenuItem::Add( CMenuItem* pNewChild )
  54. {
  55.     pChild[dwNumChildren++] = pNewChild;
  56.     pNewChild->pParent = this;
  57.  
  58.     return pNewChild;
  59. }
  60.  
  61.  
  62.  
  63. //-----------------------------------------------------------------------------
  64. // Name: Render()
  65. // Desc: 
  66. //-----------------------------------------------------------------------------
  67. HRESULT CMenuItem::Render( LPDIRECT3DDEVICE8 pd3dDevice, CD3DFont* pFont )
  68. {
  69.     // Check parameters
  70.     if( NULL==pd3dDevice || NULL==pFont )
  71.         return E_INVALIDARG;
  72.  
  73.     // Save current matrices
  74.     D3DXMATRIX matViewSaved, matProjSaved;
  75.     pd3dDevice->GetTransform( D3DTS_VIEW,       &matViewSaved );
  76.     pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProjSaved );
  77.  
  78.     // Setup new view and proj matrices for head-on viewing
  79.     D3DXMATRIX matView, matProj;
  80.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0.0f,0.0f,-30.0f),
  81.                                   &D3DXVECTOR3(0.0f,0.0f,0.0f),
  82.                                    &D3DXVECTOR3(0.0f,1.0f,0.0f) );
  83.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
  84.     pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  85.     pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  86.  
  87.     // Establish colors for selected vs. normal menu items
  88.     D3DMATERIAL8 mtrlNormal, mtrlSelected, mtrlTitle;
  89.     D3DUtil_InitMaterial( mtrlTitle,    1.0f, 0.0f, 0.0f, 1.0f );
  90.     D3DUtil_InitMaterial( mtrlNormal,   1.0f, 1.0f, 1.0f, 0.5f );
  91.     D3DUtil_InitMaterial( mtrlSelected, 1.0f, 1.0f, 0.0f, 1.0f );
  92.  
  93.     pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  94.     pd3dDevice->SetRenderState( D3DRS_AMBIENT,  0xffffffff );
  95.  
  96.     // Translate the menuitem into place
  97.     D3DXMATRIX matWorld;
  98.     D3DXMatrixScaling( &matWorld, 0.4f, 0.4f, 0.4f );
  99.     matWorld._42 = (dwNumChildren*1.0f) + 2.0f;
  100.     pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  101.  
  102.     pd3dDevice->SetMaterial( &mtrlTitle );
  103.  
  104.     // Render the menuitem's label
  105.     pFont->Render3DText( strLabel, D3DFONT_CENTERED|D3DFONT_TWOSIDED );
  106.  
  107.     // Loop through and render all menuitem lables
  108.     for( DWORD i=0; i<dwNumChildren; i++ )
  109.     {
  110.         D3DXMATRIX matWorld;
  111.         D3DXMatrixScaling( &matWorld, 0.3f, 0.3f, 0.3f );
  112.         pd3dDevice->SetMaterial( &mtrlNormal );
  113.  
  114.         // Give a different effect for selected items
  115.         if( dwSelectedMenu == i )
  116.         {
  117.             D3DXMATRIX matRotate;
  118.             D3DXMatrixRotationY( &matRotate, (D3DX_PI/3)*sinf(timeGetTime()/200.0f) );
  119.             D3DXMatrixMultiply( &matWorld, &matWorld, &matRotate );
  120.             pd3dDevice->SetMaterial( &mtrlSelected );
  121.         }
  122.  
  123.         // Translate the menuitem into place
  124.         matWorld._42 = (dwNumChildren*1.0f) - (i*2.0f);
  125.         pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  126.  
  127.         // Render the menuitem's label
  128.         pFont->Render3DText( pChild[i]->strLabel, 
  129.                              D3DFONT_CENTERED|D3DFONT_TWOSIDED );
  130.     }
  131.  
  132.     // Restore matrices
  133.     pd3dDevice->SetTransform( D3DTS_VIEW,       &matViewSaved );
  134.     pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProjSaved );
  135.  
  136.     return S_OK;
  137. }
  138.  
  139.  
  140.  
  141.  
  142.